var apiHost = wx.getStorageSync("apiHost");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    collageId : '',             //拼团id
    skuPropertyDetailList :[],  //sku属性值集合
    SkuPropertyHeight : '',     //编辑库存高度
    allInventory : '',          //批量编辑数量
    InventoryList : [],         //保存库存所需的集合
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var collageId = wx.getStorageSync("collageId")  //拼团id

    that.setData({
      collageId: collageId
    });

    that.initSkuPropertyDetail(collageId);    //规格参数 初始化

    that.countSkuPropertyHeight();
  },

  //计算编辑库存 高度
  countSkuPropertyHeight: function () {
    var that = this;
    /*获取指定元素高度*/
    var query2 = wx.createSelectorQuery();
    var BottomBtnCon = "";

    query2.select('#editInvBottomBtnCon').boundingClientRect()
    query2.exec(function (res) {
      BottomBtnCon = res[0].height;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            SkuPropertyHeight: res.windowHeight - BottomBtnCon,
          });
        }
      });
    });
  },

  /**
   * 规格参数 初始化
   */
  initSkuPropertyDetail: function (collageId){
    var that = this;
    wx.showLoading({
      title: '玩命加载中',
      icon: 'loading',
      mask: true
    });

    wx.request({
      url: apiHost + 'hpp/collage/skuPropertyDetail',
      method :'get',
      header : {
        "content-type" : "application/json"
      },
      data : {
        collageId:collageId
      },
      success:function(res){
        console.log(res)
        var code = res.data.code;
        var msg = res.data.msg;
        wx.hideLoading();
        if ("10000" == code){
          var skuPropertyDetailList = res.data.data;
          
          that.setData({
            skuPropertyDetailList: skuPropertyDetailList
          });
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
      },
      fail : function(){
        wx.hideLoading();
        wx.showToast({
          title: "网络异常",
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  /**
   * 批量编辑库存
   */
  allInventory:function(res){
    var that = this;
    var value = res.detail.value;
    that.setData({
      allInventory: value
    });
  },

  /**
   * 保存 按钮  表单提交
   */
  firstCollageBox: function (e) {
    var that = this;
    var InPidList = e.detail.value;                             //表单提交的所有值
    var InventoryList = [];                                     //保存库存的集合
    var InventoryPitStr = "";                                   //拼接所需要的字符串
    var skuPropertyDetailList = that.data.skuPropertyDetailList;//sku属性值集合

    for (var i = 0; i < skuPropertyDetailList.length;i++){
      var propertyId = "propertyId"+i ; 
      var Inventory = "Inventory" + i; 
      
      var propertyIdValue = InPidList[propertyId];    //获取对应的Id值
      var InventoryValue = InPidList[Inventory];      //获取对应的库存值

      //判断库存信息是否填写完全
      if (InventoryValue==""){
        wx.showToast({
          title: "请填写库存信息",
          icon: 'none',
          duration: 2000,
          mask: true
        });
        return
      }

      InventoryPitStr = '{ "propertyId":"' + propertyIdValue + '","inventorys":"' + InventoryValue + '" }';
      InventoryList.push(JSON.parse(InventoryPitStr));
    }

    that.saveInventorys(InventoryList);//保存库存信息
  },

  /**
   * 保存库存信息
   */
  saveInventorys: function (InventoryList) {
    var that = this;
    console.log(InventoryList)
    wx.showLoading({
      title: '玩命加载中',
      icon: 'loading',
      mask: true
    });

    wx.request({
      url: apiHost + 'hpp/collage/saveInventorys',
      method: 'post',
      header: {
        "content-type": "application/json"
      },
      data:InventoryList,
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        wx.hideLoading();
        if ("10000" == code) {
          that.goBack();//返回上一级
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: "网络异常",
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

 

  /**
   * 返回按钮 
   */
  goBack : function(){

    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });

    wx.reLaunch({
      url: '../seniorSetUp/seniorSetUp',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})